<?php
class notification_model extends CI_Model
{
	public $table_name='notification';
	function __construct()
	{
		parent::__construct();
		$config['hostname'] = 'localhost';
		$config['username'] = 'root';
		$config['password'] = '';
		$config['database'] = 'helloworld';
		$config['dbdriver'] = 'mysql';
		$config['dbprefix'] = '';
		$config['pconnect'] = TRUE;
		$config['db_debug'] = TRUE;
		$config['cache_on'] = FALSE;
		$config['cachedir'] = '';
		$config['char_set'] = 'utf8';
		$config['dbcollat'] = 'utf8_general_ci';
		$config['swap_pre'] = '';
		$config['autoinit'] = TRUE;
		$config['stricton'] = FALSE;
		//@@@@@@testers: check the database name before start!!
		$this->load->database($config);
	}


	function create_new_notice($notice_type)
	{
		global $new_hello;
		if($notice_type == 1)
		{
			$this->db->from('hello');
			$this->db->where('helloid', $this->session->userdata('helloid'));
			$q = $this->db->get();
			$a = $q->result();
			$new_hello['userid']=$a[0]->userid;
			$new_hello['helloid'] = $this->session->userdata('helloid');
			$new_hello['hello_title'] = $a[0]->title;
		}
		else if($notice_type==2)
		{
				
			$this->db->from('hello');
			$this->db->where('helloid', $this->session->userdata('helloid'));
			$q = $this->db->get();
			$a=$q->result();
				
			$this->db->from('apply');
			$this->db->where('helloid', $this->session->userdata('helloid'));
			$q = $this->db->get();
			$c=$q->result();
			$new_hello['userid'] = $c[0]->guestid;
			$new_hello['helloid'] = $this->session->userdata('helloid');
			$new_hello['hello_title'] = $a[0]->title;
		}
		$new_hello['sender_name'] = $this->session->userdata('fullname');
		$new_hello['senderid'] = $this->session->userdata('userid');
		$new_hello['is_read'] = 0;
		$new_hello['notice_type'] = $notice_type;
		$this->db->insert('notification', $new_hello);
	}
	
	function create_new_notice2($receiverid)
	{
		
		$new_hello['userid']=$receiverid;
		$new_hello['sender_name'] = $this->session->userdata('fullname');
		$new_hello['senderid'] = $this->session->userdata('userid');
		$new_hello['is_read'] = 0;
		$new_hello['notice_type'] = 3;
		$this->db->insert($this->table_name, $new_hello);
		
	}
	function create_new_notice3($friendid)
	{
		$new_hello['userid']=$friendid;
		$new_hello['sender_name'] = $this->session->userdata('fullname');
		$new_hello['senderid'] = $this->session->userdata('userid');
		$new_hello['is_read'] = 0;
		$new_hello['notice_type'] = 4;
		$this->db->insert($this->table_name, $new_hello);
	}

	
	function update($profile)
	{
		$this->db->where('noticeid', $profile['noticeid']);
		$this->db->update($this->table_name, $profile);
	}
	
	//not yet being used
	function delete($profile)
	{
		$this->db->where('noticeid', $profile['noticeid']);
		$this->db->delete($this->table_name, $profile);
	}


	function get_notice($profile)
	{
		reset($profile);

		while (list($field, $content) = each($profile))
		{
			$this->db->where($field, $content);
		}
		return $this->db->get($this->table_name);
	}


	function get_all_notice()
	{
		$this->db->where('userid',$this->session->userdata('userid'));
		$this->db->order_by("noticeid", "desc");
		$query=$this->db->get($this->table_name);
		global $data;
		if($query->num_rows() > 0)
		{
			foreach ($query->result() as $row)
			{
				if($row->is_read==0)
				{
					$data[] = $row;
				}

			}
			reset($query->result());
			foreach ($query->result() as $rows)
			{
				if($rows->is_read==1)
				{
					$data[] = $rows;
				}

			}
		}
		return $data;
	}

	
	function ChangeQueryResultToArray($query)
	{
		if($query->num_rows() > 0)
		{
			foreach ($query->result() as $row)
			{
				$data[] = $row;
			}
			return $data;
		}
	}

}

